/****************** Exercise 8 ******************
 * Write a program to demonstrate that no
 * matter how many objects you create of a
 * particular class, there is only one instance
 * of a particular static field in that class.
 ************************************************/

package thinkinginjava.everything_is_an_object;

class C2{
    static Object nonPrimitiveStaticField = new Object();;
    static int staticIntField = 0;
    int nonStaticIntField = 0;
}

/**
 *
 * @author vlad
 */
public class Exercise8 {
    public static void main(String[] args){
        C2 inst1 = new C2();
        System.out.println("inst1.staticIntField = " + inst1.staticIntField);
        System.out.println("inst1.nonStaticIntField = " + inst1.nonStaticIntField);
        System.out.println("=======================");
        inst1.staticIntField++;
        inst1.nonStaticIntField++;
        System.out.println("inst1.staticIntField = " + inst1.staticIntField);
        System.out.println("inst1.nonStaticIntField = " + inst1.nonStaticIntField);
        System.out.println("=======================");
        C2 inst2 = new C2();
        System.out.println("inst2.staticIntField = " + inst2.staticIntField);
        System.out.println("inst2.nonStaticIntField = " + inst2.nonStaticIntField);
        System.out.println("inst1.staticIntField = " + inst1.staticIntField);
        System.out.println("inst1.nonStaticIntField = " + inst1.nonStaticIntField);
        System.out.println("=======================");
        inst1.staticIntField++;
        inst1.nonStaticIntField++;
        System.out.println("inst2.staticIntField = " + inst2.staticIntField);
        System.out.println("inst2.nonStaticIntField = " + inst2.nonStaticIntField);
        System.out.println("inst1.staticIntField = " + inst1.staticIntField);
        System.out.println("inst1.nonStaticIntField = " + inst1.nonStaticIntField);
        System.out.println("=======================");
        System.out.println("(inst1.nonPrimitiveStaticField == inst2.nonPrimitiveStaticField) = "
                + (inst1.nonPrimitiveStaticField == inst2.nonPrimitiveStaticField));
    }
}
